﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model_SSResourceManager;
using System.Windows.Forms ;

namespace SSRMmonitor
{
    class CurrentActivityViewControler
    {
        private CurrentActivityView w_currentActivity;
        Timer m_timer ;
        private IDataBase currrentDb = MsSqlDb.SSRMDB;
        private ModelsManager m_manager;        

        private Queue<string> inputMesages = new Queue<string>();
        private bool inProcess = false;

        public CurrentActivityViewControler( ModelsManager _manager )
        {
            m_manager = _manager;          
        }

        public void NewView()
        {
            if (w_currentActivity == null || !w_currentActivity.Visible)
            {
                w_currentActivity = new CurrentActivityView();
                w_currentActivity.FormClosed += CurrentActivityView_FormClosed;

                m_timer = new Timer();
                m_timer.Interval = 300;
                m_timer.Tick += CheckInputList;

                AddInputMessages(currrentDb.SqlErrorMessage);

                currrentDb.DbErrEvent += CheckNewActivity;
                m_manager.messageModel.MessageEvent += CheckMessage;

                m_timer.Start();

                w_currentActivity.Show();
            }
        }

        private void CurrentActivityView_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_timer.Stop();
            w_currentActivity.Controls.Clear();
            inputMesages.Clear();
            currrentDb.DbErrEvent -= CheckNewActivity;
            m_manager.messageModel.MessageEvent -= CheckMessage;
        }

        void CheckNewActivity(object sender, DbErrEventArgs e)
        {
            AddInputMessages(e.MessageText);
            //  AddNewLabel(DateTime.Now.ToString() + " " + e.MessageText);            
        }

        void CheckMessage(object sender, MessageEventArgs e)
        {
            AddInputMessages(e.MessageText);
        }

        void AddInputMessages(string _msg)
        {
            lock (inputMesages)
            {
                inputMesages.Enqueue(DateTime.Now.ToString() + " " + _msg + "\n");
            }
        }

        public void AddNewLabel(string _lblTxt)   // add new label with message to Activity Window
        {
           /* using (Label _lbl = new Label() )
            {
                _lbl.Text = _lblTxt;
                _lbl.AutoSize = true;
                _lbl.Top = 3 + w_currentActivity.Controls.Count * (_lbl.Height + 3);
                _lbl.Left = 5;
                w_currentActivity.Controls.Add(_lbl);
                _lbl.Show();
            } */
            lock (w_currentActivity.Controls[0])
            {
                ((RichTextBox)w_currentActivity.Controls[0]).AppendText(_lblTxt);
            }
        }

        private void CheckInputList(object sender,  EventArgs e)  
        {
            if (!this.inProcess)
            {
                inProcess = true;
                lock (inputMesages)
                {                    
                    while (inputMesages.Count > 0)
                    {
                        AddNewLabel( inputMesages.Dequeue() );
                    }
                }
                inProcess = false;
            }
        }

    }
}
